package com.moxiao.yfjh.module.blind.service.memberReport;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.moxiao.yfjh.module.blind.controller.admin.memberReport.vo.MemberReportCreateReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.memberReport.vo.MemberReportExportReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.memberReport.vo.MemberReportPageReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.memberReport.vo.MemberReportUpdateReqVO;
import com.moxiao.yfjh.module.blind.convert.memberReport.MemberReportConvert;
import com.moxiao.yfjh.module.blind.dal.dataobject.comment.MemberCommentDO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.moxiao.yfjh.module.blind.controller.admin.memberReport.vo.*;
import com.moxiao.yfjh.module.blind.dal.dataobject.memberReport.MemberReportDO;
import com.moxiao.yfjh.framework.common.pojo.PageResult;

import com.moxiao.yfjh.module.blind.dal.mysql.memberReport.MemberReportMapper;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.module.blind.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 相亲交友举报 Service 实现类
 *
 * @author mx
 */
@Service
@Validated
public class MemberReportServiceImpl implements MemberReportService {

    @Resource
    private MemberReportMapper memberReportMapper;

    @Override
    public Long createMemberReport(MemberReportCreateReqVO createReqVO) {
        // 插入
        MemberReportDO memberReport = MemberReportConvert.INSTANCE.convert(createReqVO);
        memberReportMapper.insert(memberReport);
        // 返回
        return memberReport.getId();
    }

    @Override
    public void updateMemberReport(MemberReportUpdateReqVO updateReqVO) {
        // 校验存在
        validateMemberReportExists(updateReqVO.getId());
        // 更新
        MemberReportDO updateObj = MemberReportConvert.INSTANCE.convert(updateReqVO);
        memberReportMapper.updateById(updateObj);
    }

    @Override
    public void deleteMemberReport(Long id) {
        // 校验存在
        validateMemberReportExists(id);
        // 删除
        memberReportMapper.deleteById(id);
    }

    private void validateMemberReportExists(Long id) {
        if (memberReportMapper.selectById(id) == null) {
            throw exception(MEMBER_REPORT_NOT_EXISTS);
        }
    }

    @Override
    public MemberReportDO getMemberReport(Long id) {
        return memberReportMapper.selectById(id);
    }

    @Override
    public List<MemberReportDO> getMemberReportList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return memberReportMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<MemberReportDO> getMemberReportPage(MemberReportPageReqVO pageReqVO) {
        return memberReportMapper.selectPage(pageReqVO);
    }

    @Override
    public List<MemberReportDO> getMemberReportList(MemberReportExportReqVO exportReqVO) {
        return memberReportMapper.selectList(exportReqVO);
    }

    @Override
    public MemberReportDO getMemberReportTodayLast(Date todayStart, Date todayEnd) {
        LambdaQueryWrapper<MemberReportDO> lqw = Wrappers.lambdaQuery();
        if (Objects.nonNull(todayStart) && Objects.nonNull(todayEnd)) {
            lqw.between(MemberReportDO::getCreateTime, todayStart, todayEnd);
        }
        lqw.orderByDesc(MemberReportDO::getCreateTime);
        lqw.last("LIMIT " + 1);
        return memberReportMapper.selectOne(lqw);
    }

    @Override
    public Integer getBlindMemberReportNo(String reportNo) {
        return memberReportMapper.getMemberReportNo(reportNo);
    }

}
